package com.gjh.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;
import com.gjh.domain.cargo.*;
import com.gjh.service.cargo.ContractProductService;
import com.gjh.service.cargo.ExtCproductService;
import com.gjh.service.cargo.FactoryService;
import com.gjh.utils.FileUploadUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtProductController extends BeanController {
    @Reference
    private FactoryService factoryService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private ExtCproductService extCproductService;
    @Autowired
    private FileUploadUtil fileUploadUtil;


    @RequestMapping(value="/list",name="展示货物下的所有附件")
    public String list(String contractId,String contractProductId, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int size){
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, page, size);
        request.setAttribute("page",pageInfo);

        //查询所有工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/toAdd",name ="进入添加附件页面")
    public String toAdd(){
        return "cargo/contract/contract-add";
    }

    @RequestMapping(value = "/toUpdate",name = "进入修改货物下附件页面")
    public String toUpdate(String id,String contractId,String contractProductId){
        //http://localhost:8080/cargo/extCproduct/toUpdate.do?id=e94b674d-da85-4836-9b84-a27972ba4eac&contractId=a0a3d244-6327-41e3-84b6-26a348052dd4&contractProductId=32a86cdc-2800-4990-9306-18c5cf626422
        //获取当前附件对象
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        //查询所有工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/edit",name = "保存附件方法")
    public String edit(ExtCproduct extCproduct,MultipartFile productPhoto){
        try {
            String upload = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage(upload);//把七牛云返回的地址添加到表中
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取是否有id
        if(StringUtils.isEmpty(extCproduct.getId())){
            //新增
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else {
            //修改
            extCproduct.setUpdateTime(new Date());
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    @RequestMapping(value = "/delete",name = "删除货物下的附件")
    public String delete (String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }



}
